import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:thal/backstage/toolkit/utilities/color_palette.dart';

/// 加载指示器管理类
/// 统一管理全局加载状态的显示与隐藏
///
/// 使用示例：
/// ```dart
/// // 显示加载（水波纹动画）
/// LoadingCurtain.raiseCurtain(message: 'Loading...');
///
/// // 隐藏加载
/// LoadingCurtain.lowerCurtain();
///
/// // 修改动画类型：直接在 bootstrap() 中修改 indicatorType 即可
/// // 可选动画：ripple, pulse, cubeGrid, foldingCube, wave, dualRing 等
/// ```
class LoadingCurtain {
  /// 私有构造函数，防止实例化
  LoadingCurtain._();

  /// 是否已初始化
  static bool _hasInitialized = false;

  /// 初始化加载器和Toast的统一样式配置
  static void bootstrap() {
    if (_hasInitialized) return;

    EasyLoading.instance
      // 通用配置
      ..displayDuration = const Duration(milliseconds: 2000)
      ..indicatorType = EasyLoadingIndicatorType.ripple
      ..loadingStyle = EasyLoadingStyle.custom
      ..radius = 16.0
      ..backgroundColor = ColorPalette.white
      ..textColor = ColorPalette.black
      ..progressColor = ColorPalette.blue1
      ..indicatorColor = ColorPalette.blue1
      ..contentPadding = const EdgeInsets.symmetric(
        vertical: 20.0,
        horizontal: 24.0,
      )
      ..boxShadow = [
        BoxShadow(
          color: ColorPalette.blue1.withValues(alpha: 0.3),
          blurRadius: 20,
          spreadRadius: 5,
          offset: const Offset(0, 4),
        ),
      ]
      // 加载动画配置
      ..indicatorType = EasyLoadingIndicatorType.ripple
      ..indicatorSize = 50.0
      ..maskColor = ColorPalette.black.withValues(alpha: 0.6)
      ..userInteractions = false
      ..dismissOnTap = false
      // Toast配置
      ..toastPosition = EasyLoadingToastPosition.top;

    _hasInitialized = true;
  }

  /// 展示加载动画
  static void raiseCurtain({String? message}) {
    if (!_hasInitialized) bootstrap();
    EasyLoading.show(
      status: message ?? 'Loading...',
      maskType: EasyLoadingMaskType.black,
    );
  }

  /// 隐藏加载动画
  static void lowerCurtain() {
    EasyLoading.dismiss();
  }

  /// 判断是否正在显示
  static bool get isPerforming => EasyLoading.isShow;
}
